//
//  OneToOneViewController.m
//  HollyCrmSdk
//
//  Created by hollycrm on 2021/3/17.
//

#import "OneToOneViewController.h"
#import "RTCController.h"

@interface OneToOneViewController ()<TRTCDelegate>

@end

@implementation OneToOneViewController

#pragma mark rtccontroller 相关事件处理
-(void)onUserVideoAvailable:(NSString *)uid available:(BOOL)available{
    if (available) {
        [self.bigVideoView setHidden:false];
        [self.smallVideoView setHidden:false];
        [[TRTCCloud sharedInstance] startRemoteView:uid streamType:TRTCVideoStreamTypeBig view:_bigVideoView];
        [[TRTCCloud sharedInstance] startLocalPreview:true view:_smallVideoView];
    }
    else {
        //TRTCCloud.sharedInstance()?.stopRemoteView(userId)
        [[TRTCCloud sharedInstance] stopRemoteView:uid streamType:TRTCVideoStreamTypeBig];
    }
}

-(void)onUserEnter:(NSString *)uid{
    _stateLabel.text = @"通话中";
    [RTCController shareController].rtcCallState = incall;
    [self refreshPage];
}

- (void)onError:(NSString *)uid{
    [[RTCController shareController] exitRoom];
    [self dismissViewControllerAnimated:true completion:nil];
}

-(void)onUserLeave:(NSString *)uid{
    _stateLabel.text = @"对方已离开";
    [RTCController shareController].rtcCallState = leave;
    [[RTCController shareController] exitRoom];
    [self refreshPage];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self dismissViewControllerAnimated:true completion:^{
            
        }];
     });
     
}



#pragma mark *****************************************

-(instancetype)init{
   
    NSBundle * bundle = [NSBundle bundleWithPath:[[NSBundle mainBundle].bundlePath stringByAppendingPathComponent:@"HC.bundle"]];
       self = [super initWithNibName:@"OneToOneViewController" bundle:bundle];

       return self;
}

- (IBAction)switchCamera:(id)sender {
    [RTCController shareController].isFrontCamera = ![RTCController shareController].isFrontCamera;
        [[[TRTCCloud sharedInstance] getDeviceManager] switchCamera:[RTCController shareController].isFrontCamera];
    
}


//拒绝接听
- (IBAction)refuseClick:(id)sender {
    NSLog(@"refuse click");
    [[RTCController shareController] exitRoom];
    [self dismissViewControllerAnimated:true completion:nil];
}
// 扬声器点击

- (IBAction)speakerClick:(id)sender {
    NSLog(@"speaker click");
    [RTCController shareController].isSpeakerOn = ![RTCController shareController].isSpeakerOn;
    [self refreshButtonImage];
}
//拒绝接听
- (IBAction)hangUpClick:(id)sender {
    NSLog(@"hangup click");
    [[RTCController shareController] exitRoom];
    [self dismissViewControllerAnimated:true completion:nil];
}
//麦克风点击
- (IBAction)micClick:(id)sender {
    NSLog(@"mic click");
    [RTCController shareController].isMute = ![RTCController shareController].isMute;
    [self refreshButtonImage];
}
//接听点击
- (IBAction)acceptClick:(id)sender {
    NSLog(@"accept click");
    [RTCController shareController].rtcCallState = incall;
    [[RTCController shareController] enterRoomWithView:false];
    [self refreshPage];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self refreshPage];
}

#pragma mark 页面刷新
-(void)refreshPage{
    NSLog(@"refreshPage");

    if ([RTCController shareController].rtcCallState == call || [RTCController shareController].rtcCallState == incall || [RTCController shareController].rtcCallState == invite ) {
        [_bigVideoView setHidden:false];
        [_smallVideoView setHidden:false];
        [[TRTCCloud sharedInstance] stopLocalPreview];
        [[TRTCCloud sharedInstance] startLocalPreview:true view:_smallVideoView];
        [[TRTCCloud sharedInstance] startRemoteView:[RTCController shareController].touserid streamType:TRTCVideoStreamTypeBig view:_bigVideoView];
    }
    else{
        [[TRTCCloud sharedInstance] stopLocalPreview];
        [[TRTCCloud sharedInstance] stopRemoteView:[RTCController shareController].touserid streamType:TRTCVideoStreamTypeBig];
        [_bigVideoView setHidden:true];
        [_smallVideoView setHidden:true];
    }
    
    
//1呼叫中  2等待接听 3通话中
    switch ([RTCController shareController].rtcCallState) {
        case call:
            [_micBt setHidden:true];
            [_hangupBt setHidden:false];
            [_speakerBt setHidden:true];
            _stateLabel.text = @"等待对方接听";
            [_refuseBt setHidden:true];
            [_acceptBt setHidden:true];
            break;
        case invite:
            [_micBt setHidden:true];
            [_hangupBt setHidden:true];
            [_speakerBt setHidden:true];
            _stateLabel.text = @"您有新的通话请求";
            [_refuseBt setHidden:false];
            [_acceptBt setHidden:false];
            break;
        case incall:
            if ([RTCController shareController].rtcCallType == pointToPointAudio){
                
            }
            else{
                [_micBt setHidden:true];
                [_hangupBt setHidden:false];
                [_speakerBt setHidden:true];
                _stateLabel.text = @"通话中";
                [_refuseBt setHidden:true];
                [_acceptBt setHidden:true];
                [_switchCamera setHidden:false];
            }
           
            break;

        default:
            break;
    }
    [self refreshButtonImage];
}

-(void)refreshButtonImage{
    if([RTCController shareController].isSpeakerOn){
        [_speakerBt setImage:[UIImage imageNamed:@"speaker_select"]  forState:UIControlStateNormal];
        _speakerBt.layer.borderColor = UIColor.whiteColor.CGColor;
    }
    else{
        [_speakerBt setImage:[UIImage imageNamed:@"speaker_unselect"]  forState:UIControlStateNormal];
        _speakerBt.layer.borderColor = UIColor.lightGrayColor.CGColor;
    }
    if([RTCController shareController].isMute){
        [_micBt setImage:[UIImage imageNamed:@"mic_off_select"]  forState:UIControlStateNormal];
        _micBt.layer.borderColor = UIColor.whiteColor.CGColor;
    }
    else{
        [_micBt setImage:[UIImage imageNamed:@"mic_off_unselect"]  forState:UIControlStateNormal];
        _micBt.layer.borderColor = UIColor.lightGrayColor.CGColor;
    }
    _switchCamera.layer.borderColor = UIColor.whiteColor.CGColor;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
